package edu.scau.web.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.scau.core.constants.AjaxResult;
import edu.scau.core.constants.HttpStatus;
import edu.scau.pos.domain.Category;
import edu.scau.system.domain.Role;
import edu.scau.system.service.IRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/role")
@Tag(name = "权限管理", description = "用户权限管理相关接口")
public class RoleController {
    @Autowired
    private IRoleService roleService;


    @GetMapping("/page")
    @Operation(
            summary = "分页查找角色",
            description = "分页查找角色信息,访问权限:ALL",
            parameters = {
                    @Parameter(name = "pageNum", description = "页码", required = true, schema = @Schema(type = "int")),
                    @Parameter(name = "pageSize", description = "每页数据条数", required = true, schema = @Schema(type = "int"))
            },
            requestBody = @RequestBody(
                    description = "可选角色查询条件",
                    required = false,
                    content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = Role.class)
                    )
            ),
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Role.class))),
                    @ApiResponse(responseCode = "500", description = "错误", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
            }
    )
    public AjaxResult page(
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize,
            Role role
    ) {
        PageHelper.startPage(pageNum, pageSize);
        List<Role> list = roleService.selectRoleList(role);
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        return AjaxResult.success(pageInfo);
    }

    @GetMapping("/listAll")
    @Operation(summary = "获取所有角色", description = "获取所有角色信息，访问权限:ALL",
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Role.class))),
                    @ApiResponse(responseCode = "204", description = "分类不存在", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
            }
    )
    public AjaxResult listAll() {
        return AjaxResult.success(roleService.selectAllRole());
    }

    @GetMapping("/{roleId}")
    @Operation(summary = "根据ID获取角色", description = "根据角色ID获取角色信息,访问权限:ALL")
    public AjaxResult getById(@PathVariable("roleId") Long roleId) {
        return AjaxResult.success(roleService.selectRoleById(roleId));
    }

    @PreAuthorize("hasAuthority('admin')")
    @PostMapping("/insertRole")
    @Operation(summary = "新增角色", description = "新增角色信息,访问权限:admin")
    public AjaxResult add(@org.springframework.web.bind.annotation.RequestBody Role role) {
        return AjaxResult.success(roleService.insertRole(role));
    }

    @PreAuthorize("hasAuthority('admin')")
    @PutMapping("/updateRole")
    @Operation(summary = "更新角色", description = "更新角色信息,访问权限:admin")
    public AjaxResult edit(@org.springframework.web.bind.annotation.RequestBody Role role) {
        return AjaxResult.success(roleService.updateRole(role));
    }
    @PreAuthorize("hasAuthority('admin')")
    @DeleteMapping("/delete/{roleId}")
    @Operation(summary = "根据ID删除角色", description = "根据角色ID删除角色信息,访问权限:admin")
    public AjaxResult deleteById(@PathVariable Long roleId) {
        return AjaxResult.success(roleService.deleteRoleById(roleId));
    }
}
